var jwt = require("jsonwebtoken");
const verifyToken = async (ctx, next) => {
    console.log("ctx", ctx); // 获取token
    let tokenStr = ctx.header.token || ctx.header.authorization?.slice(7) || ctx.query.token;
    console.log("tokenStr", tokenStr);

    if (tokenStr) {
        var decoded = jwt.verify(tokenStr, "xxx");
        console.log("decoded", decoded);
        if (decoded) {
            ctx.user = decoded.user;
            await next();
        } else {
            ctx.body = { error: "token过期" };
        }
    } else {
        ctx.body = { error: "请先登录" };
    }
};

exports.verifyToken = verifyToken;
